﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IntroduceCompany.Singleton;

namespace IntroduceCompany.Models
{
    public class CheckModuleAttribute : FilterAttribute, IAuthorizationFilter
    {
        private readonly string _menuID;
        public CheckModuleAttribute(string menuID) 
        {
            _menuID = menuID;
        }
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string userName = HttpContext.Current.User.Identity.Name;
            if (DataSingleton.GetMenuListModels().AcceptGoToFunctionManage(userName, _menuID))
            {
                return;
            }
         //   throw new UnauthorizedAccessException("The record does not exist or you do not have permission to access it");
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{
                                       {"controller", "Error"},{"action", "NotAuthorized"}
                                       });
        }
    }
}